<template>
  <div class="home">
    <nav>
      <div style="height: 10px;"></div>
      <img src="https://image.flaticon.com/icons/svg/145/145859.svg" class="avatar" />
      <h3>Welcome {{user.name}}</h3>
      <button @click="signOut">Sign out</button>
      <div style="height: 10px;"></div>
    </nav>
    <div class="content">
      <div>
        <button style="margin: 5px;" @click="newUser">Create</button>
        <button style="margin: 5px;" @click="deleteUser">Delete</button>
      </div>
      <div style="display: flex; flex-grow: 1; flex-direction: row;">
        <div class="list">
          <list-tile v-for="item in users" :key="item.name" @click="selectedUser = item" :class="{selected: selectedUser.id == item.id}">
            <img slot="leading" style="width: 50px; height: 50px;" src="https://image.flaticon.com/icons/svg/145/145864.svg" alt="">
            <p slot="title">{{item.name}}</p>
            <p slot="subTitle">{{item.password}}</p>
          </list-tile>
        </div>
        <div class="edit">
          <input type="text" v-model="selectedUser.name"/>
          <input type="text" v-model="selectedUser.password"/>
        </div>
      </div>
    </div>
  </div>
</template>
<style scoped>
.home {
  display: flex;
  flex-grow: 1;
  flex-direction: row;
}
nav {
  width: 250px;
  background-color: rgba(37, 38, 54, 0.089);
  display: flex;
  align-items: center;
  flex-direction: column;
}
.avatar {
  width: 120px;
  height: 120px;
  border-radius: 60px;
  border: solid 1px rgba(0, 0, 0, 0.137);
}
.content {
  display: flex;
  flex-direction: column;
  flex-grow: 1;
}
.edit {
  display: flex;
  flex-grow: 1;
  flex-direction: column;
}
.list {
  width: 200px;
  display: flex;
  flex-direction: column;
  border-right: solid 1px rgba(8, 8, 8, 0.123);
}
.selected {
  color: white;
  background-color: rgb(2, 148, 92);
}
</style>
<script lang="ts">
import { Vue, Component } from "vue-property-decorator";
import { router } from "../router";
import { store } from "../store";
import ListTile from "../components/ListTile.vue";
import { Person } from '../entities/Person';

@Component({
  components: {
    ListTile
  }
})
export default class Home extends Vue {
  private index: number = 0
  private users: Person[] = []
  private selectedUser: Person = new Person("", "");
  private get user() {
    return store.state.user;
  }
  protected created(): void {
    if (this.user.name == "") {
      router.push({ name: "SignIn" });
    }
  }
  private newUser(){
    this.selectedUser = new Person(`Person ${this.index}`, "")
    this.selectedUser.id = this.index++
    this.users.push(this.selectedUser)
  }
  private deleteUser() {
    this.users = this.users.filter(item => item.id != this.selectedUser.id)
  }
  private signOut(){
    router.push({
      name: "SignIn"
    })
  }
}
</script>
